package com.sgb.kjwl.view.ui.activity.house;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityLeaseHallBinding;
import com.sgb.kjwl.model.entity.LeasePublishEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.activity.self.authentication.AuthActivity3;
import com.sgb.kjwl.view.ui.activity.self.publish.CarPublishActivity1;
import com.sgb.kjwl.view.ui.activity.self.publish.DevicePublishActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.LeaseFactoryActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.LeaseFarmActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.LeaseLandActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.LeaseMachineActivity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.sgb.kjwl.view.ui.fragment.device.DeviceListFragment3;
import com.sgb.kjwl.view.ui.fragment.machine.CarListFragment3;
import com.sgb.kjwl.view.ui.fragment.machine.MachineListFragment3;
import com.sgb.kjwl.view.widget.LeasePublishDialog;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 出租大厅
 * 租赁1.5.4 2021 8月13废弃，改为使用LeaseHallActivity2（出租。求租，集采，招采，自营合并体）
 */
@Deprecated
public class LeaseHallActivity extends AppActivity {
    private ActivityLeaseHallBinding binding;
    private FragPagerAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments;
    private final String[] titles = {"全部", "机械租赁", "设备租赁", "车辆租赁", "房产租赁", "土地租赁", "工厂租赁", "农机租赁"};
    private int type = 0;
    private int categoryFirst;
    private int categorySecond;
    private String isSelf;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, LeaseHallActivity.class);
        context.startActivity(intent);
    }

    public static void start(Context context, int type, int categoryFirst, int categorySecond) {
        Intent intent = new Intent();
        intent.putExtra("type", type);
        intent.putExtra("categoryFirst", categoryFirst);
        intent.putExtra("categorySecond", categorySecond);
        intent.setClass(context, LeaseHallActivity.class);
        context.startActivity(intent);
    }

    public static void start(Context context, String isSelf) {
        Intent intent = new Intent();
        intent.putExtra("isSelf", isSelf);
        intent.setClass(context, LeaseHallActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_lease_hall);
        binding.ivBack.setVisibility(View.VISIBLE);
        binding.ivBack.setOnClickListener(this);
        binding.tvRelease.setOnClickListener(this);

        binding.editSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.editSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    closeKeybord();
                    EventBus.getDefault().postSticky(LeaseEvent.make().setMessageTag(EventConstant.EVENT_HOUSE_FILTER).setMessageData(binding.editSearch.getText().toString()));
                }
                return false;
            }
        });
    }

    @Override
    protected void init() {
        if (getIntent() != null) {
            isSelf = getIntent().getStringExtra("isSelf");
            if ("1".equals(isSelf)) {
                binding.tvTitle.setText("自营租赁");
            }
        }
        mFragments = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            if ("全部".equals(titles[i])) {
                mFragments.add(MachineListFragment3.newInstance("", isSelf));
            } else if ("机械租赁".equals(titles[i])) {
                mFragments.add(MachineListFragment3.newInstance("1", isSelf));
            } else if ("设备租赁".equals(titles[i])) {
                mFragments.add(DeviceListFragment3.newInstance(isSelf));
            } else if ("车辆租赁".equals(titles[i])) {
                mFragments.add(CarListFragment3.newInstance(isSelf));
            } else if ("房产租赁".equals(titles[i])) {
                mFragments.add(LeaseHouseListFragment.newInstance(isSelf));
            } else if ("土地租赁".equals(titles[i])) {
                mFragments.add(LeaseLandListFragment.newInstance(isSelf));
            } else if ("工厂租赁".equals(titles[i])) {
                mFragments.add(LeaseFactoryListFragment.newInstance(isSelf));
            } else if ("农机租赁".equals(titles[i])) {
                mFragments.add(LeaseFarmListFragment.newInstance(isSelf));
            }
        }

        initExtra();
        pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), mFragments);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.viewpager.setOffscreenPageLimit(titles.length - 1);
        binding.tablayout.setViewPager(binding.viewpager, titles);
        binding.tablayout.setCurrentTab(type);

    }

    //给Fragment设置从外部传递的属性
    private void initExtra() {
        if (getIntent().getExtras() != null) {
            type = getIntent().getIntExtra("type", 0);
            categoryFirst = getIntent().getIntExtra("categoryFirst", 0);
            categorySecond = getIntent().getIntExtra("categorySecond", 0);
            // 因第0个下标数据为"不限,全部",则下标需+1才能和分类界面对应
            Bundle bundle = getIntent().getExtras();
            bundle.putInt("categoryFirst", categoryFirst);
            bundle.putInt("categorySecond", categorySecond);
            mFragments.get(type).setArguments(bundle);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_release://发布
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
//                    EventBus.getDefault().postSticky("transport_to_login");
                    showDialogs(2);
                } else {
                    boolean manager = SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH, false);
                    if (manager) {
                        showBottomDialog();
                    } else {
                        showDialogs(1);
                    }
                }
                break;
        }

    }

    //认证dialog
    private void showDialogs(int type) {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        tv_title.setVisibility(View.VISIBLE);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("温馨提示");
        if (type == 1) {
            tv_message.setText("要先认证为实名用户/企业用户才能发布");
        } else {
            tv_message.setText("未登录，请先登录。");
        }

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == 1) {
                    AuthActivity3.start(getActivity());
                } else {
                    VerificationCodeLoginActivity.start(getActivity());
                }
                roundCornerDialog.dismiss();
            }
        });

        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });

    }

    private List<LeasePublishEntity> getList() {
        List<LeasePublishEntity> list = new ArrayList<>();
        list.add(new LeasePublishEntity("机械出租", "械"));
        list.add(new LeasePublishEntity("设备出租", "设"));
        list.add(new LeasePublishEntity("车辆出租", "车"));
        list.add(new LeasePublishEntity("房产出租", "房"));
        list.add(new LeasePublishEntity("土地出租", "地"));
        list.add(new LeasePublishEntity("工厂出租", "厂"));
        list.add(new LeasePublishEntity("农机出租", "农"));
        return list;
    }

    private void showBottomDialog() {
        LeasePublishDialog leasePublishDialog = new LeasePublishDialog(getActivity(), null);
        leasePublishDialog.setData(getList());
        leasePublishDialog.setDialogClickCallBack(new LeasePublishDialog.DialogClickCallBack() {
            @Override
            public void onItemClick(String s) {
                switch (s) {
                    case "械"://机械
                        LeaseMachineActivity.start(LeaseHallActivity.this);
                        break;
                    case "设": //设备
                        DevicePublishActivity.start(LeaseHallActivity.this);
                        break;
                    case "车"://车辆
                        CarPublishActivity1.start(LeaseHallActivity.this);
                        break;
                    case "房"://房产
                        LeaseHousesActivity.start(LeaseHallActivity.this);
                        break;
                    case "地"://土地
                        LeaseLandActivity.start(LeaseHallActivity.this);
                        break;
                    case "厂"://工厂
                        LeaseFactoryActivity.start(LeaseHallActivity.this);
                        break;
                    case "农"://农机
                        LeaseFarmActivity.start(LeaseHallActivity.this);
                        break;
                }
            }
        }).show();
    }


}
